<?php
if (!defined("BASEPATH")) exit("No direct script access allowed");

class Ent2image extends MY_Model {

	function __construct() {
		parent::__construct();
		$this->tableName = "ent2image";
		$this->prefix = "e2i";
	}

	function getEnt2image($where = NULL) {

		$qs =	"
					select
						e2i_id, e2i_cou_id, e2i_city_id, e2i_ins_id, e2i_main_slider_2_ins_id, ins_name, ins_url_name, ins_presenter, e2i_image_name, e2i_image_name_original, e2i_alt, e2i_width, e2i_height, e2i_thumb_width, e2i_thumb_height, e2i_resized_width, e2i_resized_height, e2i_cropped, e2i_hash
					from
						".$this->db->dbprefix.$this->tableName."
						left join ".$this->db->dbprefix."institute on ins_id = e2i_main_slider_2_ins_id and ins_del = 0
					where
						e2i_del = 0".((!is_null($where))?(" and ".$where):(""));
		$result = $this->db->query($qs);
		$getEnt2image = array();
		if($result->num_rows()) {
			foreach ($result->result() as $row) {

				$thumb = preg_split("/\./", $row->e2i_image_name);
				$len = strlen($thumb[count($thumb)-1]);

				$getEnt2image[] = array(
					"e2iId" => $row->e2i_id,
					"e2iCouId" => $row->e2i_cou_id,
					"e2iCityId" => $row->e2i_city_id,
					"e2iInsId" => $row->e2i_ins_id,
					"e2iMainSlider2InsId" => $row->e2i_main_slider_2_ins_id,
					"e2iImageName" => $row->e2i_image_name,
					"e2iImageNameOriginal" => $row->e2i_image_name_original,
					"e2iImageNameThumb" => substr($row->e2i_image_name, 0, -($len+1))."_thumb.".$thumb[count($thumb)-1],
					"e2iImageNameResized" => substr($row->e2i_image_name, 0, -($len+1))."_resized.".$thumb[count($thumb)-1],
					"e2iAlt" => (($row->e2i_alt)?($row->e2i_alt):("")),
					"e2iWidth" => $row->e2i_width,
					"e2iHeight" => $row->e2i_height,
					"e2iThumbWidth" => $row->e2i_thumb_width,
					"e2iThumbHeight" => $row->e2i_thumb_height,
					"e2iResizedWidth" => $row->e2i_resized_width,
					"e2iResizedHeight" => $row->e2i_resized_height,
					"e2iCropped" => $row->e2i_cropped,
					"e2iImageNameCropped" => (($row->e2i_cropped == "I")?(substr($row->e2i_image_name, 0, -($len+1))."_cropped.".$thumb[count($thumb)-1]):("")),
					"e2iCroppedText" => $this->lang->line((($row->e2i_cropped == "I")?("YES"):("NO"))),
					"e2iHash" => $row->e2i_hash,
					"insName" => $row->ins_name,
					"insUrlName" => $row->ins_url_name,
					"insPresenter" => $row->ins_presenter,
				);
			}
		}

		return $getEnt2image;
	}
	
	function getEnt2imagebyinsId($insId) {
	
		$this->load->model("institute");
		$getWholeTreeIds = $this->institute->getWholeTreeIds($insId);
	
		$qs =	"
					select
						e2i_id, e2i_cou_id, e2i_city_id, e2i_ins_id, e2i_image_name, e2i_image_name_original, e2i_alt, e2i_width, e2i_height, e2i_thumb_width, e2i_thumb_height, e2i_resized_width, e2i_resized_height, e2i_cropped
					from
						".$this->db->dbprefix.$this->tableName."
						left join ".$this->db->dbprefix."major on e2i_maj_id = maj_id and maj_ins_id in (".implode(",", $getWholeTreeIds).") and maj_del = 0
						left join ".$this->db->dbprefix."institute on maj_ins_id = ins_id and ins_del = 0
					where
						e2i_del = 0 and (maj_id is not null or ins_id is not null or e2i_ins_id in (".implode(",", $getWholeTreeIds)."))
					group by
						e2i_hash
					order by
						e2i_id
					";
					//Common::pr($qs);
		$result = $this->db->query($qs);
		$getEnt2imagebyinsId = array();
		if($result->num_rows()) {
			foreach ($result->result() as $row) {

				$thumb = preg_split("/\./", $row->e2i_image_name);
				$len = strlen($thumb[count($thumb)-1]);

				$getEnt2imagebyinsId[] = array(
					"e2iId" => $row->e2i_id,
					"e2iCouId" => $row->e2i_cou_id,
					"e2iCityId" => $row->e2i_city_id,
					"e2iInsId" => $row->e2i_ins_id,
					"e2iImageName" => $row->e2i_image_name,
					"e2iImageNameOriginal" => $row->e2i_image_name_original,
					"e2iImageNameThumb" => substr($row->e2i_image_name, 0, -($len+1))."_thumb.".$thumb[count($thumb)-1],
					"e2iImageNameResized" => substr($row->e2i_image_name, 0, -($len+1))."_resized.".$thumb[count($thumb)-1],
					"e2iAlt" => (($row->e2i_alt)?($row->e2i_alt):("")),
					"e2iWidth" => $row->e2i_width,
					"e2iHeight" => $row->e2i_height,
					"e2iThumbWidth" => $row->e2i_thumb_width,
					"e2iThumbHeight" => $row->e2i_thumb_height,
					"e2iResizedWidth" => $row->e2i_resized_width,
					"e2iResizedHeight" => $row->e2i_resized_height,
					"e2iCropped" => $row->e2i_cropped,
					"e2iImageNameCropped" => (($row->e2i_cropped == "I")?(substr($row->e2i_image_name, 0, -($len+1))."_cropped.".$thumb[count($thumb)-1]):("")),
					"e2iCroppedText" => $this->lang->line((($row->e2i_cropped == "I")?("YES"):("NO"))),
				);
			}
		}

		return $getEnt2imagebyinsId;
	}

}